package com.gdsz.libbasic.utils.assist

import android.annotation.SuppressLint
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*


var y: SimpleDateFormat = SimpleDateFormat("yyyy",Locale.getDefault())
val m: SimpleDateFormat = SimpleDateFormat("MM",Locale.getDefault())
val ym: SimpleDateFormat = SimpleDateFormat("yyyy-MM",Locale.getDefault())
val d = SimpleDateFormat("dd",Locale.getDefault())
val md = SimpleDateFormat("MM-dd",Locale.getDefault())
val ymd = SimpleDateFormat("yyyy-MM-dd",Locale.getDefault())
val ymdDot = SimpleDateFormat("yyyy.MM.dd",Locale.getDefault())
val ymd_hm_Dot = SimpleDateFormat("yyyy.MM.dd HH:mm",Locale.getDefault())
val ymd_hms = SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault())
val ymd_hms_s = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS",Locale.getDefault())
val ymd_hm = SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.getDefault())
val hm = SimpleDateFormat("HH:mm",Locale.getDefault())
val md_hm = SimpleDateFormat("MM月dd日 HH:mm",Locale.getDefault())
val md_hmLink = SimpleDateFormat("MM-dd HH:mm",Locale.getDefault())
val ymd_CN = SimpleDateFormat("yyyy年MM月dd日",Locale.getDefault())
val ym_CN = SimpleDateFormat("yyyy年MM月",Locale.getDefault())
val md_CN = SimpleDateFormat("MM月dd日",Locale.getDefault())
val md_slash = SimpleDateFormat("MM/dd",Locale.getDefault())
val md_hm_slash = SimpleDateFormat("MM/dd HH:mm",Locale.getDefault())
val ymd_slash = SimpleDateFormat("yyyy/MM/dd",Locale.getDefault())
val ymd_hm_slash = SimpleDateFormat("yyyy/MM/dd HH:mm",Locale.getDefault())
val ymd_hms_slash = SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.getDefault())
val ym_dot = SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.getDefault())
val hms = SimpleDateFormat("yyyy.MM",Locale.getDefault())
val zodiacArray = arrayOf(
    "猴", "鸡", "狗", "猪", "鼠", "牛",
    "虎", "兔", "龙", "蛇", "马", "羊"
)

val constellationArray = arrayOf(
    "水瓶座", "双鱼座", "白羊座",
    "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"
)

val weekArray = arrayOf("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")

val constellationEdgeDay = intArrayOf(
    20, 19, 21, 21, 21, 22,
    23, 23, 23, 23, 22, 22
)


fun getYmd(timeInMills: Long): String {
    return ymd.format(Date(timeInMills))
}

fun getYmdSlash(timeInMills: Long): String {
    return ymd_slash.format(Date(timeInMills))
}

fun getYmdHmSlash(timeInMills: Long): String {
    return ymd_hm_slash.format(Date(timeInMills))
}

fun getYmdHmsToYmdSlash(time: String): String {
    return try {
        val date = ymd_hms.parse(time)
        ymd_slash.format(date.time)
    } catch (e: Exception) {
        ""
    }
}

fun getYmdHmsToYmdHmsSlash(time: String?): String {
    if (time == null) {
        return ""
    }
    return try {
        val date = ymd_hms.parse(time)
        ymd_hms_slash.format(date.time)
    } catch (e: Exception) {
        ""
    }
}


fun formatConversion(sf1: SimpleDateFormat, dateStr: String, sf2: SimpleDateFormat): String {
    return try {
        val date = sf1.parse(dateStr)
        sf2.format(date.time)
    } catch (e: Exception) {
        ""
    }
}


fun getYmdDot(timeInMills: Long): String {
    return ymdDot.format(Date(timeInMills))
}

fun getYmDot(timeInMills: Long): String {
    return hms.format(Date(timeInMills))
}


fun getThisMdSlash(): String {
    return md_slash.format(getCurrentDataLong())
}


fun getYmdHms(timeInMills: Long): String {
    return ymd_hms.format(Date(timeInMills))
}

fun getYmCN(timeInMills: Long): String {
    return ym_CN.format(Date(timeInMills))
}

fun getYmdHmsS(timeInMills: Long): String {
    return ymd_hms_s.format(Date(timeInMills))
}

fun getYmdHm(timeInMills: Long): String {
    return ymd_hm.format(Date(timeInMills))
}

fun getHm(timeInMills: Long): String {
    return hm.format(Date(timeInMills))
}

fun getMd(timeInMills: Long): String {
    return md.format(Date(timeInMills))
}

fun getMdHm(timeInMills: Long): String {
    return md_hm.format(Date(timeInMills))
}

fun getMdHmLink(timeInMills: Long): String {
    return md_hmLink.format(Date(timeInMills))
}

fun getM(timeInMills: Long): String {
    return m.format(Date(timeInMills))
}

fun getD(timeInMills: Long): String {
    return d.format(Date(timeInMills))
}

fun getMdHmSlash(date: Date): String {
    return md_hm_slash.format(date)
}

fun getDateByFormat(strDate: String, format: SimpleDateFormat): Date? {
    try {
        return format.parse(strDate)
    } catch (e: ParseException) {
        e.printStackTrace()
    }

    return null
}


fun getStringByMdMsSlash(strDate: String, format: SimpleDateFormat): String {
    getDateByFormat(strDate, format)?.let {
        return getMdHmSlash(it)
    }
    return ""
}

fun getStringByFormat(date: Date, format: SimpleDateFormat): String {

    try {
        return format.format(date)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return ""
}

/**
 * 字符串转date 年-月-日 时:分
 */
fun stampToDate(s: String): String {
    return ymd_hm.format(Date(s.toLong()))

}

/**
 * 字符串转date 年-月-日 时:分:秒
 */
fun stampToDateYmdHms(s: String): String {
    return ymd_hms.format(Date(s.toLong()))
}

/**
 * 字符串转date  中文单位年月日
 */
fun stampToDateCN(s: String): String {
    return ymd_CN.format(Date(s.toLong()))
}

/**
 * 字符串转date  月 日 时 分
 */
fun stampToMD(s: String): String {
    return md_hm.format(Date(s.toLong()))
}

/**
 * 字符串转date 年月日
 */
fun stampToDateYMD(s: String): String {
    return ymd.format(Date(s.toLong()))
}


fun isLastDayOfMonth(): Boolean {
    val calendar = Calendar.getInstance()
    calendar.time = Calendar.getInstance().time
    calendar[Calendar.DATE] = calendar[Calendar.DATE] + 1
    return calendar[Calendar.DAY_OF_MONTH] == 1
}

/**
 * 获取当前时间
 *
 * @return
 * @throws Exception
 */
fun getCurrentDate(): LocalDateTime {
    return LocalDateTime.now()
}

fun isFirstDay(): Boolean {
    val c = Calendar.getInstance() //可以对每个时间域单独修改
    val date = c[Calendar.DATE]
    return date == 1
}

/**
 * 获取当前时间long值
 *
 * @return
 */
fun getCurrentDataLong(): Long {
    return Date().time
}

/**
 * @param date
 * @param newFormat
 * @return
 * @throws Exception
 */
@Throws(ParseException::class)
fun getDate(date: Date, newFormat: SimpleDateFormat): Date? {
    val tempDate = newFormat.format(date)
    return newFormat.parse(tempDate)
}

fun format(date: Date, format: SimpleDateFormat): String {
    return format.format(date)
}

fun formatSecondMs(second: Int): String {
    var mm = 0
    var ss = 0
    return if (second <= 0) {
        "00:00"
    } else {
        mm = second / 60
        ss = second % 60
        unitFormat(mm) + ":" + unitFormat(ss)
    }
}


private fun unitFormat(i: Int): String {

    return if (i in 0..9) "0$i" else "" + i

}


/**
 * 对日期进行加法操作
 *
 * @param date
 * @param hours
 * @return
 */
fun addHours(date: Date, hours: Int): Date? {
    if (null != date) {
        val cal = Calendar.getInstance()
        cal.time = date
        cal.add(Calendar.HOUR_OF_DAY, hours)
        return cal.time
    }
    return null
}

/**
 * 对日期的分钟进行加法操作
 *
 * @param date
 * @param minutes
 * @return
 */
fun addMinutes(date: Date, minutes: Int): Date? {

    val cal = Calendar.getInstance()
    cal.time = date
    cal.add(Calendar.MINUTE, minutes)
    return cal.time

}

/**
 * 比较两个日期的大小
 *
 * @param date1
 * @param date2
 * @return 日期相等，返回0；前者大，返回1；后者大，返回-1；
 */
fun dateCompare(date1: Date, date2: Date): Int {
    val c1 = Calendar.getInstance()

    c1.time = date1

    val c2 = Calendar.getInstance()

    c2.time = date2

    return c1.compareTo(c2)
}

/**
 * 将UTC时间转换为本地时间
 *
 * @param utcTime
 * @param utcTimePatten
 * @param localTimePatten
 * @return 本地时间
 */
fun utc2Local(
    utcTime: String,
    utcTimePatten: SimpleDateFormat,
    localTimePatten: SimpleDateFormat
): String {
    utcTimePatten.timeZone = TimeZone.getTimeZone("UTC")
    var gpsUTCDate: Date? = null
    try {
        gpsUTCDate = utcTime.let { utcTimePatten.parse(it) }
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    localTimePatten.timeZone = TimeZone.getDefault()
    return localTimePatten.format(gpsUTCDate!!.time)
}


/**
 * 将本地时间转换为UTC
 *
 * @param localTime
 * @param localTimePatten
 * @param utcTimePatten
 * @return UTC时间
 */
fun local2utc(
    localTime: String,
    localTimePatten: SimpleDateFormat,
    utcTimePatten: SimpleDateFormat
): String {
    // 1、取得本地时间：
    val cal = Calendar.getInstance()
    cal.time = getDateByFormat(localTime, localTimePatten)
    // 2、取得时间偏移量：
    val zoneOffset = cal[Calendar.ZONE_OFFSET]
    // 3、取得夏令时差：
    val dstOffset = cal[Calendar.DST_OFFSET]
    // 4、从本地时间里扣除这些差量，即可以取得UTC时间：
    cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset))
    val year = cal[Calendar.YEAR]
    //int month = cal.get(Calendar.MONTH) + 1;
    val month = cal[Calendar.MONTH]
    val day = cal[Calendar.DAY_OF_MONTH]
    val hour = cal[Calendar.HOUR_OF_DAY]
    val minute = cal[Calendar.MINUTE]
    cal[year, month, day, hour] = minute
    return getStringByFormat(cal.time, utcTimePatten)
}

/**
 * 是否是今天
 *
 * @param timeInMills
 * @return
 */
fun isToday(timeInMills: Long): Boolean {
    val dest = getYmd(timeInMills)
    val now = getYmd(Calendar.getInstance().timeInMillis)
    return dest == now
}

/**
 * 是否是同一天
 *
 * @param aMills
 * @param bMills
 * @return
 */
fun isSameDay(aMills: Long, bMills: Long): Boolean {
    val aDay = getYmd(aMills)
    val bDay = getYmd(bMills)
    return aDay == bDay
}

/**
 * 获取年份
 *
 * @param mills
 * @return
 */
fun getYear(mills: Long): Int {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = mills
    return calendar[Calendar.YEAR]
}

/**
 * 获取月份
 *
 * @param mills
 * @return
 */
fun getMonth(mills: Long): Int {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = mills
    return calendar[Calendar.MONTH] + 1
}


/**
 * 获取月份的天数
 *
 * @param mills
 * @return
 */
fun getDaysInMonth(mills: Long): Int {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = mills
    val year = calendar[Calendar.YEAR]
    val month = calendar[Calendar.MONTH]
    return when (month) {
        Calendar.JANUARY, Calendar.MARCH, Calendar.MAY, Calendar.JULY, Calendar.AUGUST, Calendar.OCTOBER, Calendar.DECEMBER -> 31
        Calendar.APRIL, Calendar.JUNE, Calendar.SEPTEMBER, Calendar.NOVEMBER -> 30
        Calendar.FEBRUARY -> if (year % 4 == 0) 29 else 28
        else -> throw IllegalArgumentException("Invalid Month")
    }
}

/**
 * 获取星期,0-周日,1-周一，2-周二，3-周三，4-周四，5-周五，6-周六
 *
 * @param mills
 * @return
 */
fun getWeek(mills: Long): Int {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = mills
    return calendar[Calendar.DAY_OF_WEEK] - 1
}

fun getYmCn(): String {
    return ym_CN.format(getCurrentDataLong())
}


/**
 * 获取当月第一天的时间（毫秒值）
 *
 * @param mills
 * @return
 */
fun getFirstOfMonth(mills: Long): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = mills
    calendar[Calendar.DAY_OF_MONTH] = 1
    return calendar.timeInMillis
}

//fun getFirstAndLastOnMonth(dateStr: String, sdf: SimpleDateFormat) {
//    var dataArray:Array<String> = Array()
//    val monthDate = sdf.parse(dateStr)!!.time
//    dataArray[0] = getFirstOfMonth(monthDate)
//    dataArray[1] = getFirstOfMonth(monthDate)
//}

/**
 * 获取当前月第一天
 * @return Date
 */
fun getFirstOfMonth(): Date {
    val c = Calendar.getInstance()
    c.add(Calendar.MONTH, 0)
    c[Calendar.DAY_OF_MONTH] = 1
    return c.time
}

/**
 * 获取当前月最后一天
 * @return Date
 */
fun getLastOfMonth(): Date {
    val ca = Calendar.getInstance()
    ca[Calendar.DAY_OF_MONTH] = ca.getActualMaximum(Calendar.DAY_OF_MONTH)
    return ca.time
}


/**
 * @Description 根据当前日期获取本季度第一天
 * @param
 * @Throws
 * @Return java.util.Date
 * @Date 2021-08-31 15:37:11
 * @Author WangKun
 */
fun getCurrentQuarterStartTime(): Date {
    val c = Calendar.getInstance()
    val currentMonth = c[Calendar.MONTH] + 1

    var now: Date? = null
    when {
        currentMonth <= 3 -> c[Calendar.MONTH] = 0
        currentMonth <= 6 -> c[Calendar.MONTH] = 3
        currentMonth <= 9 -> c[Calendar.MONTH] = 6
        currentMonth <= 12 -> c[Calendar.MONTH] = 9
    }
    c[Calendar.DATE] = 1
    return c.time
}

/**
 * @Description 根据当前日期获取本季度最后一天
 * @param
 * @Throws
 * @Return java.util.Date
 * @Date 2021-08-31 15:30:23
 * @Author WangKun
 */
fun getCurrentQuarterEndTime(): Date {
    val cal = Calendar.getInstance()
    cal.time = getCurrentQuarterStartTime()
    cal.add(Calendar.MONTH, 2)
    cal[Calendar.DAY_OF_MONTH] = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
    cal[Calendar.HOUR_OF_DAY] = 23
    cal[Calendar.MINUTE] = 59
    cal[Calendar.SECOND] = 59
    cal[Calendar.MILLISECOND] = 999
    return cal.time
}


fun getStartTime(): Long {
    val currentDate: Calendar = GregorianCalendar()
    currentDate[Calendar.HOUR_OF_DAY] = 0
    currentDate[Calendar.MINUTE] = 0
    currentDate[Calendar.SECOND] = 0
    return currentDate.time.time
}

fun getEndTime(): Long {
    val currentDate: Calendar = GregorianCalendar()
    currentDate[Calendar.HOUR_OF_DAY] = 23
    currentDate[Calendar.MINUTE] = 59
    currentDate[Calendar.SECOND] = 59
    return currentDate.time.time
}

// 获得当天0点时间
fun getTimesMorning(date: Date): Long {
    val cal = Calendar.getInstance()
    cal.timeInMillis = date.time
    cal[Calendar.HOUR_OF_DAY] = 0
    cal[Calendar.SECOND] = 0
    cal[Calendar.MINUTE] = 0
    cal[Calendar.MILLISECOND] = 0
    return cal.timeInMillis
}

// 获得当天24点时间
fun getTimesNight(date: Date): Long {
    val cal = Calendar.getInstance()
    if (date != null) {
        cal.time = date
    }
    cal[Calendar.HOUR_OF_DAY] = 24
    cal[Calendar.SECOND] = 0
    cal[Calendar.MINUTE] = 0
    cal[Calendar.MILLISECOND] = 0
    return cal.timeInMillis
}

/**
 * 获取utc时间与本地时区相差时间小时
 *
 * @return
 */
fun getTimeZoneRawOffset(): Int {
    val time = TimeZone.getTimeZone(TimeZone.getDefault().id).rawOffset
    return time / (3600 * 1000)
}

/**
 * 得到本周周一
 *
 * @return yyyy-MM-dd
 */
fun getMondayOfThisWeek(): Date {
    val c = Calendar.getInstance()
    var dayOfWeek = c[Calendar.DAY_OF_WEEK] - 1
    if (dayOfWeek == 0) dayOfWeek = 7
    c.add(Calendar.DATE, -dayOfWeek + 1)
    return c.time
}

/**
 * 得到本周周日
 *
 * @return yyyy-MM-dd
 */
fun getSundayOfThisWeek(): Date {
    val c = Calendar.getInstance()
    var dayOfWeek = c[Calendar.DAY_OF_WEEK] - 1
    if (dayOfWeek == 0) dayOfWeek = 7
    c.add(Calendar.DATE, -dayOfWeek + 7)
    return c.time
}


/**
 * Get next week monday
 * -1 上周一   1 下周1
 * @param int
 * @return
 */
fun getNextWeekMonday(int: Int, dateStr: String): String {
    val cal = Calendar.getInstance()
    //获取本周的周一日期
    val date: Date = ymd.parse(getMondayOfWeek(dateStr))
    cal.time = date
    //获取下周一的日期,-7则是获取上周一日期
    cal.add(Calendar.DATE, 7 * int)
    return ymd.format(cal.time)

}

fun getCurrentQuarterMonth(): List<Int> {
    val c = Calendar.getInstance()
    return when (c[Calendar.MONTH] + 1) {
        in 1..3 -> {
            listOf(1, 2, 3)
        }

        in 4..6 -> {
            listOf(4, 5, 6)
        }

        in 7..9 -> {
            listOf(7, 8, 9)
        }

        in 10..12 -> {
            listOf(10, 11, 12)
        }

        else -> {
            listOf(1, 2, 3)
        }
    }
}


fun int2chineseNum(src: Int): String {
    return when (src) {
        1 -> "一"

        2 -> "二"

        3 -> "三"

        4 -> "四"

        5 -> "五"

        6 -> "六"

        7 -> "七"


        8 -> "八"


        9 -> "九"


        10 -> "十"


        11 -> "十一"


        12 -> "十二"

        else -> ""
    }
}


/**
 * 得到指定日期的周一
 *
 * @return yyyy-MM-dd
 */
fun getMondayOfWeek(dateStr: String): String {
    val c = Calendar.getInstance()
    c.time = ymd.parse(dateStr)
    var dayOfWeek = c[Calendar.DAY_OF_WEEK] - 1
    if (dayOfWeek == 0) dayOfWeek = 7
    c.add(Calendar.DATE, -dayOfWeek + 1)
    return ymd.format(c.time)
}


fun getSundayOfWeek(dateStr: String): String {
    val c = Calendar.getInstance()
    c.time = ymd.parse(dateStr)
    var dayOfWeek = c[Calendar.DAY_OF_WEEK] - 1
    if (dayOfWeek == 0) dayOfWeek = 7

    return ymd.format(c.time)
}


/**
 * Get current week all data
 * 获取当前星期所有的日历
 * @return
 */
fun getCurrentWeekAllData(dateStr: String): List<String> = ArrayList<String>().apply {

    val inputDate: Date = ymd.parse(dateStr)!!
    val calendar = Calendar.getInstance()
    calendar.time = inputDate;
    // 将日历调整到本周的第一天（星期日）
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    for (i in 0 until 7) {
        val date = calendar.time
        val formattedDate: String = ymd.format(date)
        add(formattedDate)
        calendar.add(Calendar.DAY_OF_MONTH, 1)
    }
}


/**
 * Get current week all data
 * 获取指定日期的那一周所有日期
 * @return
 */
fun getWeekAllData(dateStr: String): List<String> = ArrayList<String>().apply {
    try {
        repeat(7) {
            val c = Calendar.getInstance()
            c.time = ymd.parse(dateStr)
            var dayOfWeek = c[Calendar.DAY_OF_WEEK] - 1
            if (dayOfWeek == 0) dayOfWeek = 7
            c.add(Calendar.DATE, -dayOfWeek + it + 1)
            add(ymd.format(c.time))
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

}


/**
 * 获得指定日期的前一天
 *
 * @param specifiedDay
 * @return
 * @throws Exception
 */
fun getSpecifiedDayBefore(specifiedDay: String, num: Int): String {
    val c = Calendar.getInstance()
    var date: Date? = null
    try {
        date = ymd.parse(specifiedDay)
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    if (date != null) {
        c.time = date
    }
    val day = c[Calendar.DATE]
    c[Calendar.DATE] = day - num
    return ymd.format(c.time)
}


/**
 * 获得指定日期的后一天
 *
 * @param specifiedDay
 * @return
 */
fun getSpecifiedDayAfter(specifiedDay: String): String {
    val c = Calendar.getInstance()
    var date: Date? = null
    try {
        date = ymd.parse(specifiedDay)
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    if (date != null) {
        c.time = date
    }
    val day = c[Calendar.DATE]
    c[Calendar.DATE] = day + 1
    return ymd.format(c.time)
}

/**
 *
 * 描述:当前日期一周中第几天
 *
 * @param pTime 例如："2015-08-12"
 * @return 第几天
 */
fun dayForWeek(pTime: String): Int {
    var dayForWeek = 1
    try {
        val c = Calendar.getInstance()
        c.time = ymd.parse(pTime) as Date
        dayForWeek = 0
        dayForWeek = if (c[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
            7
        } else {
            c[Calendar.DAY_OF_WEEK] - 1
        }
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return dayForWeek
}

/**
 *
 * 描述:当前日期一月中第几天
 *
 * @param pTime 例如："2015-08-12"
 * @return 第几天
 */
fun dayForMonth(pTime: String): Int {
    try {
        val c = Calendar.getInstance()
        c.time = pTime.let { ymd.parse(it) }!!
        return c[Calendar.DAY_OF_MONTH]
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return 0
}

// 获得当前日期与本周一相差的天数
fun getMondayPlus(): Int {
    val cd = Calendar.getInstance()
    // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
    val dayOfWeek = cd[Calendar.DAY_OF_WEEK]
    return if (dayOfWeek == 1) {
        -6
    } else {
        2 - dayOfWeek
    }
}

/**
 *
 * 描述:求当前日期的上num个月的第一天
 *
 * @param date 当前日期
 * @param num  0：表示当前月  num必须大于0  例：num:2 表示上两个月
 * @return 日期
 */
fun getFirstDayOfMonth(date: Date, num: Int): Date? {
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH] - num] = 1
    return calendar.time
}


fun getFirstMoth(dateStr: String?, num: Int, sf: SimpleDateFormat): String {
    if (dateStr == null || dateStr == "") {
        return ""
    }
    val date = sf.parse(dateStr)!!
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH] + num] = 1
    return sf.format(calendar.time)
}

/**
 *
 * 描述:求当前日期的下num个月的最后一天
 *
 * @param date 当前日期
 * @param num  0：表示当前月  num必须大于0  例：num:2 表示上两个月
 * @return 日期
 */
fun getLastDayOfMonth(date: Date, num: Int): Date? {
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH] + num] = 1
    calendar.roll(Calendar.DATE, -1)
    return calendar.time
}

fun getLastMoth(dateStr: String?, num: Int, sf: SimpleDateFormat): String {
    if (dateStr == null || dateStr == "") {
        return ""
    }
    val date = sf.parse(dateStr)!!
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar[calendar[Calendar.YEAR], calendar[Calendar.MONTH] + num] = 1
    calendar.roll(Calendar.DATE, -1)
    return sf.format(calendar.time)
}


@SuppressLint("DefaultLocale")
fun getSsToHhMmSs(time: Long): String {

    //剩余秒数
    var time = time
    val hours = (time / 3600) //转换小时
    time %= 3600 //剩余秒数
    val minutes = (time / 60) //转换分钟
    time %= 60 //剩余秒数
    return String.format("%02d", hours.toInt()) + ":" + String.format(
        "%02d",
        minutes.toInt()
    ) + ":" + String.format("%02d", time)
}


@SuppressLint("DefaultLocale")
fun getHhMmSsToSs(timeStr: String): Int {
   val data =  timeStr.split(":")

   return data[0].toInt()*3600 + data[1].toInt()*60 + data[2].toInt()
}


@SuppressLint("DefaultLocale")
fun getSsToMmSs(time: Long): String {

    //剩余秒数
    var time = time

    val minutes = (time / 60).toLong() //转换分钟
    time %= 60 //剩余秒数
    return String.format(
        "%02d",
        minutes.toInt()
    ) + ":" + String.format("%02d", time)
}


fun getZodiac(year: Int): String {
    if (year < 1900) {
        return "未知"
    }
    val start = 1900
    val years = arrayOf(
        "鼠", "牛", "虎", "兔",
        "龙", "蛇", "马", "羊",
        "猴", "鸡", "狗", "猪"
    )
    return years[(year - start) % years.size]
}


/**
 * 根据日期获取生肖
 *
 * @return
 */
fun date2Zodica(time: Calendar): String {
    return zodiacArray[time[Calendar.YEAR] % 12]
}

/**
 * 根据日期获取生肖
 *
 * @return
 */
fun date2Zodica(time: String): String {
    val c = Calendar.getInstance()
    val date: Date
    try {
        date = ymd.parse(time) as Date
        c.time = date
        val zodica = date2Zodica(c)
        println("生肖：$zodica")
        return zodica
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return ""
}

/**
 * 根据日期获取星座
 *
 * @param time
 * @return
 */
fun date2Constellation(time: Calendar): String {
    var month = time[Calendar.MONTH]
    val day = time[Calendar.DAY_OF_MONTH]
    if (day < constellationEdgeDay[month]) {
        month -= 1
    }
    return if (month >= 0) {
        constellationArray[month]
    } else constellationArray[11]
    // default to return 魔羯
}

/**
 * 根据日期获取星座
 *
 * @param time
 * @return
 */
fun date2Constellation(time: String): String {
    val c = Calendar.getInstance()
    val date: Date
    try {
        date = ymd.parse(time) as Date
        c.time = date
        return date2Constellation(c)
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return ""
}

fun getAge(birthday: String): String {
    val birthdayLong = ymd.parse(birthday)!!.time
    val birthdayY: Int = getYear(birthdayLong)
    val birthdayM: Int = getMonth(birthdayLong)
    val birthdayD: Int = getDaysInMonth(birthdayLong)


    val nowLong = getCurrentDataLong()
    val nowY = getYear(nowLong)
    val nowM = getMonth(nowLong)
    val nowD = getDaysInMonth(nowLong)

    // 计算年龄
    var age: Int = nowY - birthdayY
    if (nowM < birthdayM || nowM == birthdayM && nowD <= birthdayD) {
        age--
    }


    return age.toString()
}


fun hmsToS(hms: String): Int {
    val time = hms.split(":")
    if (time.size != 3) {
        return 0
    }
    try {
        val hh = time[0]
        val mm = time[1]
        val ss = time[2]
        return (hh.toInt() * 360) + (mm.toInt() * 60) + ss.toInt()
    } catch (e: Exception) {
        e.printStackTrace()

    }
    return 0
}

/**
 * 得到这个月天数
 * @param date String
 * @return List<String>
 */
fun getMonthFullDay(date: String): List<String> {
    val fullDayList: MutableList<String> = ArrayList()
    val year = date.substring(0, 4).toInt()
    val month = date.substring(5, 7).toInt()
    val day = 1 // 所有月份从1号开始
    val cal = Calendar.getInstance() // 获得当前日期对象
    cal.clear() // 清除信息
    cal[Calendar.YEAR] = year
    cal[Calendar.MONTH] = month - 1 // 1月从0开始
    cal[Calendar.DAY_OF_MONTH] = day
    val count = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
    var j = 0
    while (j <= count - 1) {
        if (ymd.format(cal.time).equals(getLastDay(year, month))) break
        cal.add(Calendar.DAY_OF_MONTH, if (j == 0) +0 else +1)
        j++
        fullDayList.add(ymd.format(cal.time))
    }
    return fullDayList
}

fun getLastDay(year: Int, month: Int): String {
    val cal = Calendar.getInstance()
    cal[Calendar.YEAR] = year
    cal[Calendar.MONTH] = month
    cal[Calendar.DAY_OF_MONTH] = 0
    return ymd.format(cal.time)
}


fun getDayStartAndAfter(date: String): String {
    val calendar = Calendar.getInstance()
    val dateTime = ymd.parse(date)!!

    calendar.time = dateTime
    // 设置当前日期为当前月的第一天
    calendar.set(Calendar.DAY_OF_MONTH, 1)
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    calendar.set(Calendar.MILLISECOND, 0)

    // 获取当前月份的第一天时间戳（毫秒）
    val firstDayOfMonthTimestamp = calendar.timeInMillis

    // 设置当前日期为当前月的最后一天
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
    calendar.set(Calendar.HOUR_OF_DAY, 23)
    calendar.set(Calendar.MINUTE, 59)
    calendar.set(Calendar.SECOND, 59)
    calendar.set(Calendar.MILLISECOND, 999)

    // 获取当前月份的最后一天时间戳（毫秒）
    val lastDayOfMonthTimestamp = calendar.timeInMillis


    return "$firstDayOfMonthTimestamp,$lastDayOfMonthTimestamp"

}
fun getPastWeekDates(startDate: String): List<String> {

    val calendar = Calendar.getInstance()

    val pastWeekDates = mutableListOf<String>()

    // 将起始日期设置为指定日期
    val date = md.parse(startDate)
    calendar.time = date

    // 循环获取过去七天的日期
    for (i in 0 until 7) {
        val currentDate = calendar.time
        pastWeekDates.add(md.format(currentDate))
        calendar.add(Calendar.DAY_OF_YEAR, -1)
    }
    pastWeekDates.reverse()
    return pastWeekDates
}
fun getPreviousOrNextDays(date: String, days: Int): String {
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    val localDate = LocalDate.parse(date, formatter)

    // 获取前几天或后几天的日期
    val resultDate = localDate.plusDays(days.toLong())

    return resultDate.format(formatter)
}
